#include "edgebody.h"

EdgeBody::EdgeBody(b2World *world, b2Vec2 start,
                   b2Vec2 end,
                   b2Vec2 pivot_center,
                   float density,
                   float friction)
    : BodyWrapper(world, b2Shape::e_edge, b2BodyType::b2_staticBody, pivot_center, 0.f, density, friction),
      start(start), end(end)
{
    createBody();
}

bool EdgeBody::inRegion(int top, int left, int right, int bottom)
{
    b2Vec2 p0 = pivot_center + start;
    b2Vec2 p1 = pivot_center + end;
    return pointInRegion(p0, top, left, right, bottom) || pointInRegion(p1, top, left, right, bottom);
}

void EdgeBody::draw(QPainter *painter)
{
    b2Vec2 p0 = pivot_center + start;
    b2Vec2 p1 = pivot_center + end;
    painter->drawLine(QPointF(p0.x, p0.y), QPointF(p1.x, p1.y));
}

void EdgeBody::createBody()
{
    b2BodyDef bodyDef;
    bodyDef.type = body_type;
    bodyDef.position = pivot_center;
    bodyDef.angle = angle_in_radius;
    body = world->CreateBody(&bodyDef);

    b2EdgeShape shape;
    shape.Set(start, end);
    b2FixtureDef bodyFixtureDef;
    bodyFixtureDef.shape = &shape;
    bodyFixtureDef.density = density;
    bodyFixtureDef.friction = friction;
    body->CreateFixture(&bodyFixtureDef);
}
